//
//  ShowDetailViewController.m
//  QXShowing
//
//  Created by 王道道 on 2018/4/15.
//  Copyright © 2018年 doll_dream. All rights reserved.
//

#define kCycleViewWidth       App_Frame_Width-15*2
#define kCycleViewHeight      App_Frame_Width * 180/345
#define kHeadInfoHeight       130
#define kHeadHeight           kCycleViewHeight+kHeadInfoHeight+30
#define kBtnWidth  (App_Frame_Width-35-25-25)/2

#import "ShowDetailViewController.h"

#import "VTMagicController.h"
#import "BannerViewCell.h"
#import "BannerCycleView.h"

#import "CompanyInfoViewController.h"
#import "CompanyProductListViewController.h"

#import "UIButton+ClickAction.h"
#import "CompanyInfoModel.h"

static NSString *const listVC1 = @"listVC1";
static NSString *const listVC2 = @"listVC2";
static NSString * const reusableProductViewIdentifier = @"BannerViewCell";

@interface ShowDetailViewController ()<VTMagicViewDelegate,VTMagicViewDataSource,BannerCycleViewDelegate,BannerCycleViewDataSource>
{
    CompanyInfoViewController *listVCa;
}

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) VTMagicController *magicController;
@property (nonatomic, strong) BannerCycleView *cycleView;

@property (nonatomic, assign) CGFloat contentOffsetY;

//@property (nonatomic, strong) UIImageView *photoIV;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UILabel *numberLabel;

@property (nonatomic, strong) UIButton *chatBtn;
@property (nonatomic, strong) UIButton *callBtn;

@property (nonatomic, strong) NSArray *cycleArray;
@property (nonatomic, strong) CompanyInfoModel *dataModel;

@end

@implementation ShowDetailViewController

#pragma mark - Lazy
- (VTMagicController *)magicController {
    if (_magicController == nil) {
        _magicController = [[VTMagicController alloc]init];
        _magicController.magicView.frame = CGRectMake(0, kHeadHeight, App_Frame_Width, App_Frame_Height - kNavbarAndStatusBar);
//        _magicController.magicView.navigationColor = [UIColor whiteColor];
        _magicController.magicView.navigationColor = Bg_Color_EFEFEF;

        _magicController.magicView.backgroundColor = [UIColor whiteColor];
        _magicController.magicView.layoutStyle = VTLayoutStyleDivide;
        _magicController.magicView.switchStyle = VTSwitchStyleDefault;
//        _magicController.magicView.sliderColor = Main_Color;
        _magicController.magicView.sliderColor = Bg_Color_333333;
        _magicController.magicView.itemScale = 1.2;
        _magicController.magicView.sliderWidth = 20;
        _magicController.magicView.sliderHeight = 3;
        _magicController.magicView.scrollEnabled = YES;
        _magicController.magicView.delegate = self;
        _magicController.magicView.dataSource = self;
    }
    return _magicController;
}

- (BannerCycleView *)cycleView {
    if (_cycleView == nil) {
        _cycleView = [[BannerCycleView alloc] initWithFrame:CGRectMake(15,10, kCycleViewWidth, kCycleViewHeight)];
        _cycleView.dataSource = self;
        _cycleView.delegate = self;
        _cycleView.showPageControl = YES;
        _cycleView.pageControlPosition = BannerCyclePageControlPositionCenter;
        _cycleView.timeInterval = 3.f;
        _cycleView.itemSpace = 30;
        [_cycleView registerClass:[BannerViewCell class] forCellWithReuseIdentifier:reusableProductViewIdentifier];
    }
    return _cycleView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"企业名录";
    [self initNavBarBackBtn];
    
    [self setUpUI];
    [self setUpHeadView];
    
    [self requestPostBH_Exhibition_getPicByBoothId];
    [self requestPostBH_Exhibition_getCompanyInfoByBoothId];
}

- (void)setUpUI {
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, App_Frame_Height)];
    [self.view addSubview:self.scrollView];
    self.scrollView.delegate = self;
    self.scrollView.bounces = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
//    self.scrollView.contentSize = CGSizeMake(0, App_Frame_Height + kHeadHeight - kNavbarAndStatusBar - kBarHeight);
//    self.scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(310, 0, 0, 0);
    self.scrollView.contentSize = CGSizeMake(0, App_Frame_Height + kHeadHeight - kBarHeight);
    self.scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(kHeadHeight, 0, 0, 0);
    
    [self addChildViewController:self.magicController];
    [self.scrollView addSubview:self.magicController.magicView];
    [self.magicController.magicView reloadData];
    
    self.chatBtn = [[UIButton alloc] init];
    self.chatBtn.backgroundColor = Bg_Color_FFFFFF;
    [self.chatBtn setTitle:@"找他聊聊" forState:UIControlStateNormal];
    [self.chatBtn setTitleColor:Text_Color_333333 forState:UIControlStateNormal];
    [self.chatBtn setImage:[UIImage imageNamed:@"home-16"] forState:UIControlStateNormal];
    [self.chatBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)];
    [self.chatBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
    self.chatBtn.layer.masksToBounds = YES;
    self.chatBtn.layer.cornerRadius = 5;
    self.chatBtn.layer.borderColor = Bg_Color_333333.CGColor;
    self.chatBtn.layer.borderWidth = 1;
    [self.view addSubview:self.chatBtn];
    [self.chatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(25);
//        make.top.equalTo(self.infoTV.mas_bottom).offset(50);//70
        make.bottom.equalTo(self.view.mas_bottom).offset(-50);
        make.height.offset(45);
        make.width.offset(kBtnWidth);
    }];
    
    self.callBtn = [[UIButton alloc] init];
    self.callBtn.backgroundColor = Bg_Color_FFFFFF;
    [self.callBtn setTitle:@"拨打电话" forState:UIControlStateNormal];
    [self.callBtn setTitleColor:Text_Color_333333 forState:UIControlStateNormal];
    [self.callBtn setImage:[UIImage imageNamed:@"home-10"] forState:UIControlStateNormal];
    [self.callBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)];
    [self.callBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
    self.callBtn.layer.masksToBounds = YES;
    self.callBtn.layer.cornerRadius = 5;
    self.callBtn.layer.borderColor = Bg_Color_333333.CGColor;
    self.callBtn.layer.borderWidth = 1;
    [self.callBtn addTarget:self action:@selector(callClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.callBtn];
    [self.callBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-25);
        make.top.equalTo(self.chatBtn.mas_top);
        make.bottom.equalTo(self.chatBtn.mas_bottom);
        make.height.offset(45);
        make.width.offset(kBtnWidth);
    }];
    self.callBtn.hidden = YES;
    self.callBtn.userInteractionEnabled = NO;
    
}

- (void)setUpHeadView {
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, kHeadHeight)];
//    self.headView.backgroundColor = [UIColor redColor];
    [self.scrollView addSubview:self.headView];
    
    [self.headView addSubview:self.cycleView];

    UILabel *bgLabel = [[UILabel alloc] init];
    bgLabel.backgroundColor = Bg_Color_EFEFEF;
    [self.headView addSubview:bgLabel];
    [bgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headView.mas_left);
        make.centerX.equalTo(self.headView);
        make.height.offset(8);
        make.top.equalTo(self.cycleView.mas_bottom).offset(15);
    }];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = Bg_Color_FEFDFD;
    [self.headView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(kHeadInfoHeight);
        make.top.equalTo(bgLabel.mas_bottom);
        make.left.equalTo(self.headView.mas_left);
        make.centerX.equalTo(self.headView);
    }];
    
//    self.photoIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home-5"]];
//    [bgView addSubview:self.photoIV];
//    [self.photoIV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(bgView.mas_left).offset(15);
//        make.top.equalTo(bgView.mas_top).offset(20);
//        make.height.width.offset(90);
//    }];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @"上海企线信息科技有限公司";
//    self.titleLabel.numberOfLines = 0;
    self.titleLabel.textColor = Text_Color_000000;
    self.titleLabel.font = BHFont_Size_15;
    self.titleLabel.adjustsFontSizeToFitWidth = YES;
    [bgView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(self.photoIV.mas_right).offset(20);//36
//        make.top.equalTo(self.photoIV);
        make.left.equalTo(bgView.mas_left).offset(15);
        make.top.equalTo(bgView.mas_top).offset(20);
        make.right.equalTo(bgView.mas_right).offset(-10);
    }];
    
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.text = @"联系人：张先生";
    self.nameLabel.textColor = Text_Color_282828;
    self.nameLabel.font = BHFont_Size_12;
    self.nameLabel.adjustsFontSizeToFitWidth = YES;
    [bgView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.right.equalTo(self.titleLabel.mas_right);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);//15
    }];
    
    self.phoneLabel = [[UILabel alloc] init];
    self.phoneLabel.text = @"联系电话：17834393020";
    self.phoneLabel.textColor = Text_Color_282828;
    self.phoneLabel.font = BHFont_Size_12;
    self.phoneLabel.adjustsFontSizeToFitWidth = YES;
    [bgView addSubview:self.phoneLabel];
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.right.equalTo(self.titleLabel.mas_right);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(10);//15
    }];
    self.phoneLabel.hidden = YES;
    
    self.numberLabel = [[UILabel alloc] init];
    self.numberLabel.text = @"展位号：A-1035";
    self.numberLabel.textColor = Text_Color_282828;
    self.numberLabel.font = BHFont_Size_12;
    self.numberLabel.adjustsFontSizeToFitWidth = YES;
    [bgView addSubview:self.numberLabel];
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.right.equalTo(self.titleLabel.mas_right);
//        make.top.equalTo(self.phoneLabel.mas_bottom).offset(10);//15
        make.top.equalTo(self.phoneLabel.mas_bottom).offset(-5);//15
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)callClick:(UIButton *)sender {
    if (![BHSettingUtil dataAndStringIsNull:self.dataModel.phone]) {
        [sender callAction:self.dataModel.phone];
    }
}

#pragma mark - BannerCycleViewDelegate,BannerCycleViewDataSource
- (NSInteger)numberOfRowsInCycleView:(BannerCycleView *)cycleView {
    if (self.cycleArray.count == 0) {
        return 1;
    } else {
        return self.cycleArray.count;
    }
//        return MAX(1, self.cycleArray.count);
//    return 3;
}

- (UICollectionViewCell *)cycleView:(BannerCycleView *)cycleView cellForItemAtRow:(NSInteger)row {
    BannerViewCell *cell = (BannerViewCell *)[cycleView dequeueReusableCellWithReuseIdentifier:reusableProductViewIdentifier forRow:row];
    cell.image.contentMode = UIViewContentModeScaleAspectFill;
    if (self.cycleArray.count == 0) {
        cell.image.image = [UIImage imageNamed:@"home-a-10"];
    }else {
//        RoastingModel *model = self.homePageModel.SlideModel[row];
        NSString *picStr = self.cycleArray[row];
        if (![BHSettingUtil dataAndStringIsNull:picStr]) {
            [cell.image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", BH_PicPreString, picStr]] placeholderImage:[UIImage imageNamed:@"home-a-10"]];
        }
    }
    return cell;
}

- (CGSize)cycleView:(BannerCycleView *)cycleView sizeForItemAtRow:(NSInteger)row {
    return CGSizeMake(kCycleViewWidth, kCycleViewHeight);
}

- (void)cycleView:(BannerCycleView *)cycleView didSelectItemAtRow:(NSInteger)row {
    //    [BHPushViewObject pushSuperview:self success:nil params:self.homePageModel.SlideModel[row]];
}

#pragma mark - VTMagicViewDelegate,VTMagicViewDataSource
- (UIButton *)magicView:(VTMagicView *)magicView menuItemAtIndex:(NSUInteger)itemIndex {
    static NSString *itemIdentifier = @"itemIdentifier";
    UIButton *menuItem = [magicView dequeueReusableItemWithIdentifier:itemIdentifier];
    if (!menuItem) {
        menuItem = [UIButton buttonWithType:UIButtonTypeCustom];
        [menuItem setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [menuItem setTitleColor:Text_Color_333333 forState:UIControlStateSelected];
        menuItem.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:15.f];
    }
    return menuItem;
}

- (NSArray<NSString *> *)menuTitlesForMagicView:(VTMagicView *)magicView {
    return @[@"介绍",@"产品介绍"];
}


- (UIViewController *)magicView:(VTMagicView *)magicView viewControllerAtPage:(NSUInteger)pageIndex {
    if (pageIndex == 0) {
        listVCa = [magicView dequeueReusablePageWithIdentifier:listVC1];
        if (!listVCa) {
            listVCa = [[CompanyInfoViewController alloc] init];
        }
        listVCa.dataModel = self.dataModel;
        return listVCa;
    }else {
        CompanyProductListViewController *listVCb = [magicView dequeueReusablePageWithIdentifier:listVC2];
        if (!listVCb) {
            listVCb = [[CompanyProductListViewController alloc] init];
        }
        listVCb.Id = self.companyModel.company_id;
        return listVCb;
    }
}

- (void)magicView:(VTMagicView *)magicView viewDidAppear:(__kindof UIViewController *)viewController atPage:(NSUInteger)pageIndex {
//    [self.menuView publicPersonalSelect:pageIndex];
//    NSLog(@"======%ld",pageIndex);
}

#pragma mark UIScrollViewDelegate
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    CGFloat contentOffsetY = scrollView.contentOffset.y;
//    self.contentOffsetY = contentOffsetY;
//    CGFloat contentOffsetMinY = 290 - kNavbarAndStatusBar;
////    self.headView.alpha = 1 - contentOffsetY/contentOffsetMinY;
//    self.magicController.magicView.frame = CGRectMake(0, 0, App_Frame_Width, App_Frame_Height + contentOffsetY);
////    [self.navView scrollViewDidScroll:contentOffsetY];
//}

#pragma mark - 接口
- (void)_loadData:(id)result {    
    self.cycleArray = (NSArray *)result;
    [self.cycleView reloadData];
}

- (void)requestPostBH_Exhibition_getPicByBoothId {
    [[BHAlertUtil alertManager]showLoadingView:self.view];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:self.companyModel.booth_id forKey:@"booth_id"];
    
    [PPHTTPRequest requestPostBH_Exhibition_getPicByBoothId:^(id response, BOOL success) {
        if (success) {
            if ([response isKindOfClass:[NSArray class]]) {
                [self _loadData:response];
            }
        }
        else {
//            @WeakObj(self);
//            [self.cycleView.placeholderShow(YES) actionWithBlock:^{
//                [selfWeak requestPostBH_Exhibition_getPicByBoothId];
//            }];
        }
        [[BHAlertUtil alertManager]hiddenLoading];
    } params:params];
}

#pragma mark - 接口
- (void)_loadInfoData:(id)result {
    self.dataModel = [CompanyInfoModel yy_modelWithJSON:result];
    
    
    self.titleLabel.text = [BHSettingUtil dataAndStringIsNull:self.dataModel.company_name]?@"":self.dataModel.company_name;
    self.nameLabel.text = [BHSettingUtil dataAndStringIsNull:self.dataModel.contacts]?@"":[NSString stringWithFormat:@"联系人：%@", self.dataModel.contacts];
    self.phoneLabel.text = [BHSettingUtil dataAndStringIsNull:self.dataModel.phone]?@"":[NSString stringWithFormat:@"联系电话：%@", self.dataModel.phone];
    self.phoneLabel.hidden = YES;
    self.numberLabel.text = [BHSettingUtil dataAndStringIsNull:self.dataModel.booth_name]?@"":[NSString stringWithFormat:@"展位号：%@", self.dataModel.booth_name];
    
    listVCa.dataModel = self.dataModel;
}

//根据展位ID获取企业详细信息
- (void)requestPostBH_Exhibition_getCompanyInfoByBoothId {
    [[BHAlertUtil alertManager]showLoadingView:self.view];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:self.companyModel.booth_id forKey:@"booth_id"];
    
    [PPHTTPRequest requestPostBH_Exhibition_getCompanyInfoByBoothId:^(id response, BOOL success) {
        if (success) {
            [self _loadInfoData:response];
        }
        else {
            //            @WeakObj(self);
            //            [self.cycleView.placeholderShow(YES) actionWithBlock:^{
            //                [selfWeak requestPostBH_Exhibition_getPicByBoothId];
            //            }];
        }
        [[BHAlertUtil alertManager]hiddenLoading];
    } params:params];
}

@end
